home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / net / socket~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  5.5 KB  |  206 lines

  1. /*
  2.  * @(#)Socket.java    1.16 96/01/10 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.net;
  21.  
  22. import java.io.InputStream;
  23. import java.io.OutputStream;
  24. import java.io.IOException;
  25. import java.io.InterruptedIOException;
  26.  
  27. /**
  28.  * The client Socket class. It uses a SocketImpl
  29.  * to implement the actual socket operations. It is done this way 
  30.  * so that you are able to change socket implementations depending 
  31.  * on the kind of firewall that is used. You can change socket
  32.  * implementations by setting the SocketImplFactory.
  33.  *
  34.  * @version     1.16, 01/10/96
  35.  * @author     Jonathan Payne
  36.  * @author     Arthur van Hoff
  37.  */
  38. public final 
  39. class Socket {
  40.     /**
  41.      * The implementation of this Socket.
  42.      */
  43.     SocketImpl impl;
  44.  
  45.     /**
  46.      * Creates an unconnected socket. Note: this method
  47.      * should not be public.
  48.      */
  49.     Socket() {
  50.     impl = (factory != null) ? factory.createSocketImpl() : new PlainSocketImpl();
  51.     }
  52.  
  53.     /** 
  54.      * Creates a stream socket and connects it to the specified port on
  55.      * the specified host.
  56.      * @param host the host
  57.      * @param port the port
  58.      */
  59.     public Socket(String host, int port)
  60.     throws UnknownHostException, IOException
  61.     {
  62.     this(host, port, true);
  63.     }
  64.  
  65.     /** 
  66.      * Creates a socket and connects it to the specified port on
  67.      * the specified host. The last argument lets you specify whether
  68.      * you want a stream or datagram socket.
  69.      * @param host the specified host
  70.      * @param port the specified port
  71.      * @param stream a boolean indicating whether this is a stream 
  72.      * or datagram socket
  73.      */
  74.     public Socket(String host, int port, boolean stream) throws IOException {
  75.     this();
  76.  
  77.     String hostCopy = new String(host);
  78.  
  79.     SecurityManager security = System.getSecurityManager();
  80.     if (security != null) {
  81.         security.checkConnect(hostCopy, port);
  82.     }
  83.  
  84.     try {
  85.         impl.create(stream);
  86.         impl.connect(hostCopy, port);
  87.     } catch (IOException e) {
  88.         impl.close();
  89.         throw e;
  90.     }
  91.     }
  92.  
  93.     /** 
  94.      * Creates a stream socket and connects it to the specified address on
  95.      * the specified port. 
  96.      * @param address the specified address
  97.      * @param port the specified port
  98.      */
  99.     public Socket(InetAddress address, int port) throws IOException {
  100.     this(address, port, true);
  101.     }
  102.  
  103.     /** 
  104.      * Creates a socket and connects it to the specified address on
  105.      * the specified port. The last argument lets you specify whether
  106.      * you want a stream or datagram socket.
  107.      * @param address the specified address
  108.      * @param port the specified port
  109.      * @param stream a boolean indicating whether this is a stream 
  110.      * or datagram socket
  111.      */
  112.     public Socket(InetAddress address, int port, boolean stream) 
  113.     throws IOException
  114.     {
  115.     this();
  116.  
  117.     SecurityManager security = System.getSecurityManager();
  118.     if (security != null) {
  119.         security.checkConnect(address.getHostName(), port);
  120.     }
  121.  
  122.     try {
  123.         impl.create(stream);
  124.         impl.connect(address, port);
  125.     } catch (SocketException e) {
  126.         impl.close();
  127.         throw e;
  128.     }
  129.     }
  130.  
  131.     /**
  132.      * Gets the address to which the socket is connected.
  133.      */
  134.     public InetAddress getInetAddress() {
  135.     return impl.getInetAddress();
  136.     }
  137.  
  138.     /**
  139.      * Gets the remote port to which the socket is connected.
  140.      */
  141.     public int getPort() {
  142.     return impl.getPort();
  143.     }
  144.  
  145.     /**
  146.      * Gets the local port to which the socket is connected.
  147.      */
  148.     public int getLocalPort() {
  149.     return impl.getLocalPort();
  150.     }
  151.  
  152.     /**
  153.      * Gets an InputStream for this socket.
  154.      */
  155.     public InputStream getInputStream() throws IOException {
  156.     return impl.getInputStream();
  157.     }
  158.  
  159.     /**
  160.      * Gets an OutputStream for this socket.
  161.      */
  162.     public OutputStream getOutputStream() throws IOException {
  163.     return impl.getOutputStream();
  164.     }
  165.  
  166.     /**
  167.      * Closes the socket.
  168.      */
  169.     public synchronized void close() throws IOException {
  170.     impl.close();
  171.     }
  172.  
  173.     /**
  174.      * Converts the Socket to a String.
  175.      */
  176.     public String toString() {
  177.     return "Socket[addr=" + impl.getInetAddress() +
  178.         ",port=" + impl.getPort() + 
  179.         ",localport=" + impl.getLocalPort() + "]";
  180.     }
  181.  
  182.     /**
  183.      * The factory for all client sockets.
  184.      */
  185.     private static SocketImplFactory factory;
  186.  
  187.     /**
  188.      * Sets the system's client SocketImplFactory. The factory can 
  189.      * be specified only once.
  190.      * @param fac the desired factory
  191.      * @exception SocketException If the factory is already defined.
  192.      */
  193.     public static synchronized void setSocketImplFactory(SocketImplFactory fac)
  194.     throws IOException
  195.     {
  196.     if (factory != null) {
  197.         throw new SocketException("factory already defined");
  198.     }
  199.     SecurityManager security = System.getSecurityManager();
  200.     if (security != null) {
  201.         security.checkSetFactory();
  202.     }
  203.     factory = fac;
  204.     }
  205. }
  206.